package name.jiaosha.air.system.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.system.domain.Brand;
import name.jiaosha.air.system.service.ISystemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

/**
 * 品牌类别控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/sys")
public class BrandController {

	@Autowired
	private ISystemService systemService;

	@RequestMapping("/barndList")
	public ModelAndView getMyBugList() {

		PageInfo<Brand> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<Brand> list = new ArrayList<Brand>();
			for (int i = 1; i <= 20; i++) {
				Brand brand = new Brand();
				brand.setId(i);
				brand.setName("科龙" + i);
				brand.setPinyin(PinyinToolkit.cn2FirstSpell(brand.getName()));
				list.add(brand);
			}

		} else {

			pageInfo = systemService.getBrandListByComId(1,
					AppConstants.DEFAULT_SIZE);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createBrand")
	public ModelAndView createBrand(@RequestParam String name) {
		if (!AppConstants.DEBUG) {

		} else {

			Brand brand = new Brand();

			brand.setName(name.trim());
			systemService.createBrand(brand);

		}
		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateBrand")
	public ModelAndView updateBrand(@RequestParam int id,
			@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Brand brand = systemService.getBrandById(id);
			if (brand != null) {

				brand.setName(name.trim());
				systemService.updateBrand(brand);
			}

		}
		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}
}
